package com.tbit.main.pojo;

import com.tbit.main.util.StringUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author LMY
 * @create 2022-07-05 13:47
 * 摄像头配置表
 */
@Data
@NoArgsConstructor
public class Camera {
    /**
     * 区域id
     */
    private Integer accountId;
    /**
     * 设备编号
     */
    private String machineNO;
    /**
     * 设备功能
     */
    private String function;
    /**
     * 拍照时间
     */
    private String photographTime;

    public Camera(Integer accountId, String machineNO, String softVersion) {
        super();
        this.accountId = accountId;
        this.machineNO = machineNO;
        String function = "";
        Long s = getFunFlag(softVersion, "S-");
        if (testFun(s, 8)) {
            function += "1";
        }
        if (testFun(s, 12)) {
            if (function != null) {
                function += ",2";
            } else {
                function += "2";
            }
        }

        if (testFun(s, 9)) {
            if (function != null) {
                function += ",3";
            } else {
                function += "3";
            }
        }
        this.function = function;
    }

    /**
     * 拍照质量：0：低质量（默认）  1：中质量  2：高质量
     */


    public static boolean testFun(long i, int index) {
        return ((1 << index) & i) != 0;
    }

    /**
     * NS_TBIT_WA-209D_R.62.0.3_11-28-2019 13:51:57 &T650_G.C.6531E_GD_UBLOX_ACC_BLE_ECU_OTA_
     * HARDINFO-0000_
     * SOFTWAREFUNC-0000
     *
     * @return
     */
    public static Long getFunFlag(String ver, String name) {
        String[] strs = ver.split("_");
        for (String str : strs) {
            if (str.startsWith(name)) {
                String iStr = str.substring(name.length(), str.length());
                return Long.parseLong(iStr, 16);
            }
        }
        return 0L;
    }

    public static Camera functionString(Camera camera) {
        List<Integer> functionS = StringUtil.getIntegerList(camera.getFunction());
        String function = "";
        for (Integer i : functionS) {
            if (i.equals(1)) {
                function += "闯红灯检测功能；";
            }
            if (i.equals(2)) {
                function += "逆行检测功能；";
            }
            if (i.equals(3)) {
                function += "非机动车道骑行检测功能；";
            }
        }
        camera.setFunction(function);
        return camera;
    }

    public static void main(String[] args) {
        Camera camera = new Camera(1, "1", "NS_TBIT_WD-219_R.2.0.4_08-5-2022 11:45:59 &T650_G.C.OPENCPU_ZB_WM-005-005-005_ATGM_ACC_BLE_ECU_N58_OTA_H-0081_S-1300");
        System.out.println(camera.toString());
    }

}
